
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;

using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace UserManagement.Gui.AspNet
{
	public class UserAppFunctionDisplay : Syrinx.Gui.AspNet.Nitobi2.NitobiBizObjDisplay
	{
		protected IBizObjList m_companyUserGroups;
        protected IBizObj m_user;

		public UserAppFunctionDisplay(): base(){}
		public UserAppFunctionDisplay(ActiveWidget widget) : base(widget) { }
		
		void setupUserAppFunctionDisplay()
		{
			if (m_companyUserGroups != null)
				foreach (IBizObj companyUserGroup in m_companyUserGroups)
				{
					CheckBox cb = (CheckBox)this.FindControl("cb1" + companyUserGroup.get("id").ToString().Replace("-", ""));
					if (cb != null)
						cb.Checked = false;
				}

			if (m_user != null)
			{
				IBizObjList usrUsrGroups = (IBizObjList)m_user.get("groups");
				foreach (IBizObj userGroup in usrUsrGroups)
				{
					CheckBox cb = (CheckBox)this.FindControl("cb1" + userGroup.get("id").ToString().Replace("-", ""));
					if (cb != null)
						cb.Checked = true;
				}
			}
		}
		
		public void processCompanyUserGroups(HtmlBlockInfo info)
		{
			if(m_companyUserGroups != null)
				foreach (IBizObj userGroup in m_companyUserGroups)
				{
					string id = userGroup.get("id").ToString().Replace("-", "");
					info.generateBlock(id);
					
					CheckBox cb = (CheckBox)FindControl("cb1" + id);
					if(cb!=null)
						cb.Text = (string)userGroup.get("name");
				}
		}

		public override void displayBizObj(IBizObj user)
		{
			if (user != null)
			{
				m_user = user;
				setupUserAppFunctionDisplay();
			}
		}
		public override void updateBizObj(IBizObj user)
		{
			if (user != null)
			{
				IBizObjList usrUsrGroups = (IBizObjList)user.get("groups");
				usrUsrGroups.Clear();
				foreach (IBizObj companyUserGroup in m_companyUserGroups)
				{
					CheckBox cb = (CheckBox)this.FindControl("cb1" + companyUserGroup.get("id").ToString().Replace("-", ""));
					if (cb != null && cb.Checked)
						usrUsrGroups.insertNewMakeCopy(companyUserGroup);
				}
				m_user = user;
			}
		}				
	}
}
